//
//  ViewController.m
//  HACRouter
//
//  Created by Chengjie on 2019/4/25.
//  Copyright © 2019 Happy. All rights reserved.
//

#import "ViewController.h"
#import <HACRouter/HACRouter.h>
#import "NSObject+HACInvocation.h"

@interface ViewController ()
@property (nonatomic, copy) void(^complete)(NSString *name, NSInteger age);
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, copy) NSString *wish;
@end

@implementation ViewController

HAC_INIT_ROUTER(详情页-姓名-年龄, goods_detail, ViewController, (NSString *)name, (NSInteger)age) {
    self = [super init];
    if (self) {
        _name = [name copy];
        _age = age;
    }
    return self;
}

HAC_INIT_ROUTER(详情页, goods_detail, ViewController) {
    self = [super init];
    if (self) {
        
    }
    return self;
}

HAC_INIT_ROUTER(详情页-回调, goods_detail, ViewController, (NSString *)name, (NSInteger)age, (void(^)(NSString *name, NSInteger age))complete) {
    self = [super init];
    if (self) {
        self.complete = complete;
        {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.tag = 100;
            [button setTitle:@"测试原生跳转" forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            button.backgroundColor = [UIColor redColor];
            button.frame = CGRectMake(100, 100, 180, 44);
            [self.view addSubview:button];
            [button addTarget:self action:@selector(buttonDidTapping:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.tag = 200;
            [button setTitle:@"测试URL跳转" forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            button.backgroundColor = [UIColor redColor];
            button.frame = CGRectMake(100, 200, 180, 44);
            [self.view addSubview:button];
            [button addTarget:self action:@selector(buttonDidTapping:) forControlEvents:UIControlEventTouchUpInside];
        }
        [self.view setBackgroundColor:[UIColor purpleColor]];
    }
    return self;
}

HAC_INIT_ROUTER(详情页-姓名-年龄, goods_detail, ViewController, (NSString *)name, (NSString *)wish) {
    self = [super init];
    if (self) {
        _name = [name copy];
        _wish = [wish copy];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.name = @"Jack.long";
    self.age = 18;
    [self testIMP];
    [self testRouterUrls];
    self.title = NSStringFromClass([self class]);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (self.complete) {
        self.complete(self.name, self.age);
    }
}

#pragma mark -Test

- (void)testIMP {
    Class cls = NSClassFromString(@"HACTempModel");
    
    id obj = [[cls alloc] hac_performInitSelector:@selector(init_with_some:any:) withArguments:@[@"good", @"idea"]];
    NSLog(@"%@: %@ %@", obj, [obj valueForKey:@"some"], [obj valueForKey:@"any"]);
    
    NSLog(@"%s", @encode(NSString *));
    NSLog(@"%s", @encode(NSNumber *));
}

- (void)testRouterUrls {
    NSString *testUrls = @"https://www.baidu.com/goods/detail?name=流沙&wish=您好！！！&age=18";
    testUrls = [testUrls stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    ViewController *vc = [HACRouter routerWithUrls:testUrls];
    NSLog(@"%@:\n %@  %ld  %@",vc , vc.name, vc.age, vc.wish);
}

HAC_USE_ROUTER(自定义控制器, goods_custom, (NSString *)title)
- (void)buttonDidTapping:(UIButton *)sender {
    if (sender.tag == 100) {
        UIViewController *testVc = hac_use_path_goods_custom_controller_with_title(@"原生跳转");
        [self.navigationController pushViewController:testVc animated:YES];
    } else if (sender.tag == 200) {
        NSString *testUrls = @"https://www.baidu.com/goods/custom?name=流沙&wish=您好！！！&age=18&title=URL跳转";
        testUrls = [testUrls stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        UIViewController *testVc = [HACRouter routerWithUrls:testUrls customParser:nil];
        if (testVc) {
           [self.navigationController pushViewController:testVc animated:YES];
        }
    }

}

@end
